﻿@using Huirui.Cavan.Domain.MainBoundedContext.SystemModule
@model Huirui.Cavan.Presentation.ViewModel.SystemModule.ISysActionViewModel
@{Layout = null;}
@using (Html.BeginForm(ActionConst.AUTHORIZE, ControllerConst.SYSACTION))
{
    <div class="tool_bar">
        <input id="authorize_save" type="submit" style="display: none;" />
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-save'" onclick="javascript:$('#authorize_save').click()">保存</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-copy'" onclick="getChecked()">复制选中项</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-collapse'" onclick="toggleHeard()">折叠/展开</a>
    </div>
    <table class="form_heard">
        <tr>
            <td class="c100">
                <label>用户对应职员:</label>
            </td>
            <td class="c160">
                @Html.DropDownList("UserAccountID", this.Model.AuthorizeEmployees, new { id = "sysaction_employee" })
            </td>
            <td class="c160">
                @Html.CheckBoxFor(m => m.OnlyQuerySelfData, new { id= "OnlyQuerySelfData"})@Html.Raw("只可查看自己做的数据")
            </td>
            <td class="c160">
                @Html.CheckBoxFor(m=>m.CannotQueryPurchaseCostAndAmount, new { id= "CannotQueryPurchaseCostAndAmount"})@Html.Raw("不能查看采购单价和金额")
            </td>
            <td class="c160">
                @Html.CheckBoxFor(m=>m.CannotQuerySalesCostAndAmount, new { id= "CannotQuerySalesCostAndAmount"})@Html.Raw("不能查看销售单价和金额")
            </td>
            @Html.HiddenFor(m => m.PermissionActions, new { id= "permissionActions"})
        </tr>
    </table>
}
<table id="sysaction_dg">
</table>
<div id="sysactiontb">
    <a href="#" class="easyui-linkbutton" iconcls="icon-save" plain="true" onclick="javascript:alert('引出')">引出</a>
</div>
<script type="text/javascript">
    $(function () {
        $.extend($.fn.datagrid.methods, {
            actionGetChecked: function (jq) {
                var rr = [];
                var rows = jq.datagrid('getRows');
                jq.datagrid('getPanel').find('div.datagrid-cell input:checked').each(function () {
                    var index = $(this).parents('tr:first').attr('datagrid-row-index');
                    rr.push(rows[index]);
                });
                return rr;
            }
        });

        $("form[action^='/sysaction/authorize']").submit(function () {
            var selected = $('#sysaction_dg').datagrid('actionGetChecked');

            if (selected.length) {
                $("#permissionActions").val(JSON.stringify(selected));
            }

            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function (data) {
                    if (data.IsSuccess) {
                        alert(data.Messages[0]);
                    }
                },
                error: function () {
                    alert('系统繁忙，请稍候再试');
                }
            })

            return false;
        });

        $('#sysaction_dg').datagrid({
            class: 'easyui-datagrid',
            title: "权限列表",
            idField: 'ActionID',
            toolbar: '#sysactiontb',
            fitColumns: true,
            columns: [[
              { field: 'ModuleName', title: '模块', width: 100 },
              {
                  field: 'ActionPart1', title: '功能列表', width: 100,
                  formatter: function (value, row, index) {
                      if (value.length > 0)
                      {
                          return value + '<input type="checkbox" name="ParentCheckBox"/>';
                      }
                      return value;
                  }
              },
              { field: 'ActionPart2', title: '操作', width: 100 },
              {
                  field: 'HasPermission', title: '授权', width: 100,
                  formatter: function (value, row, index) {
                      if (value == false) {
                          return '<input type="checkbox" name="HasPermission" value=' + value + ' controller='+ row.ControllerID + ' action='+ row.ActionID + '>';
                      }
                      return '<input type="checkbox" name="HasPermission" checked="' + value + '" value=' + value + ' controller=' + row.ControllerID + ' action=' + row.ActionID + '>';
                  }
              }
            ]],
            onClickCell: function (rowIndex, field, value) {
                $("#sysaction_dg").datagrid("selectRow", rowIndex);
                var row = $("#sysaction_dg").datagrid("getSelected");
                if (field == "ActionPart1")
                {
                    $("tr[datagrid-row-index]").find("input[name='HasPermission']").each(function () {
                        if ($(this).attr('controller') == row.ControllerID) {
                            if ($("tr[datagrid-row-index='" + rowIndex + "']").find("input[name='ParentCheckBox']").attr("checked") == "checked") {
                                $(this).attr("checked", true);
                            }
                            else {
                                $(this).attr("checked", false);
                            }
                        }
                    });
                }
                else if (field == "HasPermission")
                {
                    if ($("tr[datagrid-row-index='" + rowIndex + "']").find("input[name='HasPermission']").attr("checked") == "checked")
                    {
                        var dataObj = eval("(" + row.IncludeAction + ")");
                        $.each(dataObj.Actions, function(idx, item){
                            $("tr[datagrid-row-index]").find("input[name='HasPermission']").each(function () {
                                if ($(this).attr('action') == item.ID) {
                                    $(this).attr("checked", true);
                                }
                            });
                        }) ;
                    }
                }
            }
        });

        function loadData() {
            $('#sysaction_dg').datagrid('loadData', { total: 0, rows: [] });
            $.ajax({
                url: '/sysaction/list/' + $("select#sysaction_employee").val(),
                type: 'Post',
                success: function (result) {
                    if (result.total > 0) {
                        $("#OnlyQuerySelfData").attr("checked", result.OnlyQuerySelfData);
                        $("#CannotQueryPurchaseCostAndAmount").attr("checked", result.CannotQueryPurchaseCostAndAmount);
                        $("#CannotQuerySalesCostAndAmount").attr("checked", result.CannotQuerySalesCostAndAmount);
                        $('#sysaction_dg').datagrid({
                            data: result
                        });
                    }
                },
                error: function () {
                    alert('系统繁忙，请稍候再试');
                }
            })

            return false;
        }

        $("select#sysaction_employee").bind("change", function () {
            loadData();
        });

        $("input[name='ParentAction']").each(function () {
            alert('ok');
            //$(this).bind("click", function () {
               
            //});
        })
    });
</script>
